<?php
function booking($which,$where,$bud){
include "../../share/lock.php";
$conn = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

$arr = array();
$sql="SELECT * from cr where cr_id= '$which' AND da = '$where' AND sta!= 2 AND building = '$bud'";
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
array_push($arr,$row['s_time'],$row['e_time']);
}
asort($arr);
$narr = "[";
foreach($arr as $x=>$xv){
$narr = $narr.$xv; 
$narr = $narr.",";
}
$narr = $narr."]";
$newid = $which.'-'.$where;
echo '
<div class="modal fade" id="modal-container-'.$which.'-'.$where.'" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">预约教室</h4>
			</div>
			<div class="modal-body">
				<form class="form-horizontal" role="form" method="post" action="../../actions/stu/book_a.php">
					<div class="form-group"><br/>
						<label for="inputEmail3" class="col-sm-2 control-label">教学楼</label>
						<div class="col-sm-10">
							<input type="hidden" name="bud" type="input" value="'.$bud.'" class="form-control"/>'.$bud.'
						</div>
					</div>
					<div class="form-group"><br/>
						<label for="inputEmail3" class="col-sm-2 control-label">教室编号</label>
						<div class="col-sm-10">
							<input type="hidden" name="where" type="input" value="'.$which.'" class="form-control"/>'.$which.'
						</div>
					</div>
					<div class="form-group"><br/>
						<label for="inputEmail3" class="col-sm-2 control-label">日期</label>
						<div class="col-sm-10">
							<input type="hidden" name="date" type="input" value="'.$where.'" class="form-control"/>'.$where.'
						</div>
					</div>
					<div class="form-group"><br/>
						<label for="inputEmail3" class="col-sm-2 control-label">学号</label>
						<div class="col-sm-10">
							<input name="stid" value="'.$login_session.'" type="hidden" class="form-control"/>'.$login_session.'
						</div>
					</div>
<div class="form-group"><br/>
						<label for="inputEmail3" class="col-sm-2 control-label">事由</label>
						<div class="col-sm-10">
							<input required="required" type="input" name="why" class="form-control"/>
						</div>
					</div>
					<div class="form-group"><br/>
						<label for="inputEmail3" class="col-sm-2 control-label">时间</label>
						<div class="col-sm-10">
							<select onchange="check('.$narr.',\''.$newid.'\')" id="'.$newid.'-st" name="st"><option disabled selected value="0">请选择时间</option>';
$sql="SELECT * from cr where cr_id= '$which' AND da = '$where' AND sta!= 2 AND building = '$bud'";
$result=mysqli_query($conn,$sql);
$farr = array();
$earr = array();
$marr = array();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
	array_push($farr,$row['s_time']);
	array_push($earr,$row['e_time']);
	array_push($marr,$row['s_time'],$row['e_time']);
}
if(count($marr)!=0){
$max=count($marr);
for($x=0;$x<$max;$x=$x+2){
	if(($marr[$x]+1)==$marr[$x+1]){
		;
	}
	else{
		for($c=$marr[$x];$c<$marr[$x+1];$c++){
			array_push($marr,$c);
		}
	}
}
for($i=7;$i<23;$i++){
	if(in_array($i,$marr)){
		if(in_array($i,$earr)){
			if(in_array($i,$farr)){
				echo "<option disabled='disabled'>".$i."</option>";
			}
			else{
				echo "<option>".$i."</option>";
			}
		}
		else{
			echo "<option disabled='disabled'>".$i."</option>";
		}
	}
	else{
		echo "<option>".$i."</option>";
	}
}
$newid = $which.'-'.$where;
echo '</select><select onchange="check('.$narr.',\''.$newid.'\')" id="'.$newid.'-et" name="et"><option disabled selected value="0">请选择时间</option>';
for($i=8;$i<24;$i++){
	if(in_array($i,$marr)){
		if(in_array($i,$farr)){
			if(in_array($i,$earr)){
				echo "<option disabled='disabled'>".$i."</option>";
			}
			else{
				echo "<option>".$i."</option>";
			}
		}
		else{
			echo "<option disabled='disabled'>".$i."</option>";
		}
	}
	else{
		echo "<option>".$i."</option>";
	}
}
}
else{
	for($i=7;$i<23;$i++){
		echo "<option>".$i."</option>";
	}
	echo '</select><select onchange="check('.$narr.',\''.$newid.'\')" id="'.$newid.'-et" name="et"><option disabled selected value="0">请选择时间</option>';
	for($i=8;$i<24;$i++){
		echo "<option>".$i."</option>";
	}
}
echo '</select>
<label id="'.$newid.'-label" style="color:red">*选择时间后方可提交</label>
						</div>
					</div>
					<div class="modal-footer">
						<button type="summit" id="'.$newid.'-btn" class="btn btn-primary" disabled=false>提交</button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>';
}
?>
 